package com.baide.sanguanheyi.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @ClassName DataSourceConfig
 * @Description TODO
 * @Author wangbing
 * @Date 2020/5/18 11:07
 **/
@Configuration
public class DataSourceConfig {

    @Bean(name = "masterDataSource")
    @Qualifier("dataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource master() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "otherDataSource")
    @Qualifier("otherDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.other")
    public DataSource other() {
        return DataSourceBuilder.create().build();
    }
    @Bean("otherJdbcTemplate")
    public JdbcTemplate otherJdbcTemplate(@Qualifier("otherDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}